package fl.android.ui;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Rect {

	private static float[] vertices = { 0f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 0f,
			1f, 0f };
	private static short[] indices = { 0, 1, 2, 0, 2, 3 };
	private static ByteBuffer vbb, ibb;
	private static FloatBuffer vertexBuffer;
	private static ShortBuffer indexBuffer;

	static {
		vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}

	public static void render(GL10 gl, float x, float y, float z, float wid,
			float hei, float[] color) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glPushMatrix();

		gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		gl.glScalef(wid, hei, 1f);
		gl.glColor4f(color[0], color[1], color[2], color[3]);
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
				GL10.GL_UNSIGNED_SHORT, indexBuffer);
		gl.glPopMatrix();
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
}
